﻿using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.PhieuDieuTraCoSo
{
    public partial class frmXuLyViPham : frmDetail
    {
        public frmXuLyViPham()
        {
            InitializeComponent();
            Text = @"Thêm Xử lý vi phạm";
            dtNgay.Focus();
        }

        public frmXuLyViPham(int id)
        {
            InitializeComponent();
            Text = @"Sửa Xử lý vi phạm";
            dtNgay.Focus();
            _fromtype = Constant.TypeForm.Edit;
            _id = id;

            var biz = new XuLyViPhamBiz();
            var model = biz.GetItemById(id) as Model.XuLyViPham;
            if (model == null) return;

            dtNgay.Value = model.Ngay;
            txtDoiTuongViPham.Text = model.DoiTuongViPham;
            txtNoiDung.Text = model.NoiDung;
            drlNhomHanhVi.SelectedValue = model.NhomHanhVi;
            txtCanhCao.Text = model.CanhCao;
            txtPhatTien.Text = model.PhatTien;
            dtNgayTamDinhChi.Value = model.NgayTamDinhChi;
            dtNgayPhucHoi.Value = model.NgayPhucHoi;
            txtDinhChi.Text = model.DinhChi;
            txtBienPhapXuLyKhac.Text = model.BienPhapXuLyKhac;
        }

        private void frmXuLyViPham_Load(object sender, System.EventArgs e)
        {
            LoadNhomHanhVi();
        }

        private void LoadNhomHanhVi()
        {
            var biz = new NhomHanhViBiz();
            drlNhomHanhVi.DataSource = biz.GetAll();
            drlNhomHanhVi.DisplayMember = "Ten";
        }

        #region Validation

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtDoiTuongViPham.Text))
            {
                errorProvider.SetError(txtDoiTuongViPham, "Bạn chưa nhập Đối tượng vi phạm. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }
        #endregion

        #region Insert

        protected override void Insert()
        {
            var context = new Context();
            var model = new Model.XuLyViPham();

            var nhomhanhviBiz = new NhomHanhViBiz(context);
            var nhomHanhViItem = drlNhomHanhVi.SelectedValue as Model.NhomHanhVi;
            model.NhomHanhVi = nhomhanhviBiz.GetItemById(nhomHanhViItem.Id) as Model.NhomHanhVi;

            model.Ngay = dtNgay.Value;
            model.DoiTuongViPham = txtDoiTuongViPham.Text.Trim();
            model.NoiDung = txtNoiDung.Text.Trim();
            model.CanhCao = txtCanhCao.Text.Trim();
            model.PhatTien = txtPhatTien.Text.Trim();
            model.NgayTamDinhChi = dtNgayTamDinhChi.Value;
            model.NgayPhucHoi = dtNgayPhucHoi.Value;
            model.DinhChi = txtDinhChi.Text.Trim();
            model.BienPhapXuLyKhac = txtBienPhapXuLyKhac.Text.Trim();
            var biz = new XuLyViPhamBiz(context);
            biz.Insert(model);
        }

        #endregion

        #region Edit

        protected override void Edit()
        {
            var context = new Context();
            var biz = new XuLyViPhamBiz(context);
            var item = biz.GetItemById(_id) as Model.XuLyViPham;
            if (item != null)
            {
                var nhomhanhviBiz = new NhomHanhViBiz(context);
                var nhomHanhViItem = drlNhomHanhVi.SelectedValue as Model.NhomHanhVi;
                item.NhomHanhVi = nhomhanhviBiz.GetItemById(nhomHanhViItem.Id) as Model.NhomHanhVi;

                item.Ngay = dtNgay.Value;
                item.DoiTuongViPham = txtDoiTuongViPham.Text.Trim();
                item.NoiDung = txtNoiDung.Text.Trim();
                item.CanhCao = txtCanhCao.Text.Trim();
                item.PhatTien = txtPhatTien.Text.Trim();
                item.NgayTamDinhChi = dtNgayTamDinhChi.Value;
                item.NgayPhucHoi = dtNgayPhucHoi.Value;
                item.DinhChi = txtDinhChi.Text.Trim();
                item.BienPhapXuLyKhac = txtBienPhapXuLyKhac.Text.Trim();
                biz.Update(item);
            }
        }

        #endregion
    }
}